#include "storage.h"
#include "./plugin/aws/awsStorage.h"
#include <QCommandLineParser>
#include <QApplication>
#include <QDebug>
#include <QProcess>

void copyFileOrDir(const QString& aBucket, const QJsonObject& aConfig, const QString& aSource, const QString& aTarget){
    auto clds = rea2::pipeline::input(aSource, "", std::make_shared<rea2::scopeCache>(rea2::Json("config", aConfig)), true)->asyncCall(aBucket + "listFiles")->scope()->data<std::vector<QString>>("data");
    if (!clds.size()) {
      auto stm = rea2::pipeline::input(false, "", std::make_shared<rea2::scopeCache>(rea2::Json("path", aSource, "config", aConfig)), true)->asyncCall(aBucket + "readByteArray");
      if (stm->data()) {
          stm->setData(false)->scope()->cache("path", aTarget + "/" + aSource.split("/").last())->cache("config", aConfig);
          stm->asyncCall("writeByteArray");
          qDebug() << aTarget << ":::" << aSource << ":::" << stm->data();
      }
    } else {
      auto src = aSource;
      if (src.endsWith("/"))
          src = src.mid(0, src.length() - 1);
      for (auto i : clds)
        copyFileOrDir(aBucket, aConfig, src + "/" + i, aTarget + "/" + src.split("/").last());
    }
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    std::this_thread::sleep_for(std::chrono::milliseconds(2000));
    QCommandLineParser psr;
    auto opt_ip = QCommandLineOption("ip", QCoreApplication::translate("main", "storage ip"), "value");
    auto opt_access = QCommandLineOption("access", QCoreApplication::translate("main", "storage access"), "value");
    auto opt_secret = QCommandLineOption("secret", QCoreApplication::translate("main", "storage secret"), "value");
    auto opt_bucket = QCommandLineOption("bucket", QCoreApplication::translate("main", "storage bucket"), "value");
    auto opt_source = QCommandLineOption("source", QCoreApplication::translate("main", "storage source"), "value");
    auto opt_target = QCommandLineOption("target", QCoreApplication::translate("main", "storage target"), "value");
    psr.addOptions({opt_ip, opt_access, opt_secret, opt_bucket, opt_source, opt_target});
    if (!psr.parse(QCoreApplication::arguments())){
        qDebug() << "hi";
        return 1;
    }

    if (!psr.isSet(opt_ip) || !psr.isSet(opt_access) || !psr.isSet(opt_secret)
            || !psr.isSet(opt_bucket) || !psr.isSet(opt_source) || !psr.isSet(opt_target)){
        return 1;
    }

    auto ip = psr.value(opt_ip), access = psr.value(opt_access),
            bucket = psr.value(opt_bucket), secret = psr.value(opt_secret),
            src = psr.value(opt_source), tgt = psr.value(opt_target);
    auto ssl = ip.indexOf("https") >= 0;

    rea2::fsStorage stg;
    stg.initialize();
    awsStorage stg2(bucket);
    stg2.initialize();
    auto cfg = rea2::Json("root", bucket,
                          "ip", ip,
                          "access", access,
                          "secret", secret,
                          "ssl", ssl);
    auto srcs = src.split(";"), tgts = tgt.split(";");
    QString cur_src, cur_tgt;
    for (auto i = 0; i < srcs.length(); ++i){
        cur_src = srcs[i];
        if (i < tgts.length())
            cur_tgt = tgts[i];
        copyFileOrDir(bucket, cfg, cur_src, cur_tgt);
    }

    QProcess::startDetached("DeepInspection.exe");
    return 0;
}
